package com.sx.pipeline.ui.report

import android.Manifest
import android.graphics.BitmapFactory
import android.text.TextUtils
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RoutePath.Chat.CHAT_REPORT
import com.sx.pipeline.databinding.ActRechargeBinding
import com.sx.pipeline.databinding.ActReportBinding
import com.sx.pipeline.utils.CompressHelper
import com.sx.pipeline.utils.GlideEngine
import com.sx.pipeline.utils.PermissionHelper

@Route(path = CHAT_REPORT)
class ChatReportActivity : CYDataBindingBaseActivity<ActReportBinding,ChatReportVM>(R.layout.act_report) {
    @JvmField
    @Autowired
    var type: Int = 0 //0 群 1用户

    @JvmField
    @Autowired
    var rId: String = ""

    override fun initData() {
        super.initData()
        mBinding.vm=mViewModel
        initProductImages()
        mBinding.tvSubmit.setOnClickListener {
            if(TextUtils.isEmpty(mViewModel.contentLiveData.value)){
                ToastUtils.showShort("请描述您的举报内容或者留下您的联系方式")
                return@setOnClickListener
            }
            mViewModel.reqReport(type,rId,mBinding.imageProduct.imagePath)
        }
    }

    override fun initViewModel() {
        super.initViewModel()
        //上传的图片
        observe(mViewModel.uploadDataEvent) {
            mBinding.imageProduct.addImage(it)
        }
    }

    private fun initProductImages() {
        // 上传图片
        mBinding?.imageProduct!!.initAdapter(3, 3)
        mBinding?.imageProduct!!.setImageUploadListener {
            PermissionHelper.requestTip(this,"上次图片需要授权相机和读写权限",object :
                PermissionUtils.SimpleCallback{
                override fun onGranted() {
                    PictureSelector.create(this@ChatReportActivity)
                        .openGallery(SelectMimeType.ofImage())
                        .setImageEngine(GlideEngine.createGlideEngine())
                        .setMaxSelectNum(3 - mBinding.imageProduct.imagePath.size)
                        .forResult(object : OnResultCallbackListener<LocalMedia?> {
                            override fun onResult(result: ArrayList<LocalMedia?>?) {
                                //循环上传图片
                                for (i in 0 until result?.size!!) {
                                    Log.d("okhttp", "" + result[i]?.realPath)
                                    //压缩图片
                                    val inputBitmap = BitmapFactory.decodeFile(result[i]?.realPath)
                                    val compressFile = CompressHelper.compressQuality(
                                        this@ChatReportActivity, inputBitmap
                                    )
                                    mViewModel.uploadImage(compressFile)
                                }
                            }

                            override fun onCancel() {}
                        })

                }

                override fun onDenied() {
                    ToastUtils.showShort("请授权后再继续操作")
                }
            }, Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA)
        }
    }
}